package com.yunhai.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunhai.common.core.controller.BaseController;
import com.yunhai.common.core.domain.AjaxResult;
import com.yunhai.common.core.page.TableDataInfo;
import com.yunhai.common.enums.PartyType;
import com.yunhai.system.domain.InvestorRelation;
import com.yunhai.system.domain.Party;
import com.yunhai.system.service.IPartyService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 党的建设Controller
 *
 * @author jxj
 * @date 2021-01-21
 */
@RestController
@RequestMapping("/api/party")
public class PartyController extends BaseController {
    @Autowired
    private IPartyService partyService;

    /**
     * 基本概述
     *
     * @return
     */
    @GetMapping("basic")
    public AjaxResult basic() {
        QueryWrapper<Party> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", PartyType.Basic.getCode());
        queryWrapper.orderByDesc("create_time");
        List<Party> list = partyService.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            return AjaxResult.success(list.get(0));
        }
        return AjaxResult.success();
    }

    /**
     * 党建动态
     *
     * @return
     */
    @PostMapping("dynamic")
    public TableDataInfo dynamic(@RequestBody Map<String, Object> param) {
        Long page = MapUtils.getLong(param, "page", 1L);
        Long limit = MapUtils.getLong(param, "limit", 10L);
        QueryWrapper<Party> partyQueryWrapper = new QueryWrapper<>();
        partyQueryWrapper.eq("type", PartyType.Dynamic.getCode());
        partyQueryWrapper.orderByDesc("create_time");
        IPage<Party> pageResult = partyService.page(new Page<>(page, limit), partyQueryWrapper);
        return new TableDataInfo(pageResult.getRecords(), (int) pageResult.getTotal());
    }


}
